#include "MainWindow.h"



MainWindow::MainWindow(QObject* parent) : QMainWindow()
{
	this->parent=parent;	
	initializeComponents();
}

void MainWindow::initializeComponents()
{	

	setWindowTitle("NIA Audio Player");
	initializeMenuBar();
	initializeCentralWidget();
	initializeStatusBar();
}

void MainWindow::initializeMenuBar()
{
	// File menu
	menuFile=menuBar()->addMenu(tr("&File"));
		actionOpenFiles=new QAction(tr("Import &Files"), menuFile);
		//actionOpenDir=new QAction(tr("&Import Directory"), menuFile);
		actionQuit=new QAction(tr("&Quit"), menuFile);
		
		menuFile->addAction(actionOpenFiles);
		//menuFile->addAction(actionOpenDir);
		menuFile->addAction(actionQuit);
	
	/*// Edit menu
	menuEdit=menuBar()->addMenu(tr("&Edit"));*/
	
	// Playlists menu
	menuPlaylists=menuBar()->addMenu(tr("&Playlists"));
		actionNewPlaylist=new QAction(tr("&New Playlist"), menuPlaylists);
		actionOpenPlaylist=new QAction(tr("&Open Playlist"), menuPlaylists);
		actionSavePlaylist=new QAction(tr("&Save Playlist"), menuPlaylists);
		menuPlaylists->addAction(actionNewPlaylist);
		menuPlaylists->addAction(actionOpenPlaylist);
		menuPlaylists->addAction(actionSavePlaylist);
	
	/*// Tools menu
	menuTools=menuBar()->addMenu(tr("&Tools"));*/
	
	// Help menu
	menuHelp=menuBar()->addMenu(tr("&Help"));
		actionAbout=new QAction(tr("&About NIA Audio Player"), menuHelp);
		connect(actionAbout, SIGNAL(triggered()), this, SLOT(onActionAboutTriggered()));
		
		actionAboutQt=new QAction(tr("About &Qt"), menuHelp);
		connect(actionAboutQt, SIGNAL(triggered()), qApp, SLOT(aboutQt()));
		
		menuHelp->addAction(actionAbout);
		menuHelp->addSeparator();
		menuHelp->addAction(actionAboutQt);
}

void MainWindow::initializeCentralWidget()
{
	// Instantiating the central widget
	widgetCentral=new QWidget();
		// Setting its layout
		layoutCentral=new QVBoxLayout();
		widgetCentral->setLayout(layoutCentral);
	// Applying the widget to the main window
	setCentralWidget(widgetCentral);
	
	initializeToolBarTop();
	initializeTabWidget();
	initializeToolBarBottom();
	layoutCentral->addWidget(toolBarTop);
	layoutCentral->addWidget(tabWidget);
	layoutCentral->addWidget(toolBarBottom);
}

void MainWindow::initializeToolBarTop()
{
	toolBarTop=new QToolBar();
		
		labelSearch=new QLabel(tr("Search :  "));
		toolBarTop->addWidget(labelSearch);
		
		lineEditSearch=new QLineEdit();
		toolBarTop->addWidget(lineEditSearch);
		
		toolBarTop->addSeparator();
		
		actionRandom=new QAction(tr("Shuffle"), toolBarTop);
		actionRandom->setCheckable(true);
		toolBarTop->addAction(actionRandom);
		actionRandom->setIcon(QIcon("icons/media-playlist-shuffle.png"));

		
		actionRepeat=new QAction(tr("Repeat"), toolBarTop);
		actionRepeat->setCheckable(true);
		toolBarTop->addAction(actionRepeat);
		actionRepeat->setIcon(QIcon("icons/media-playlist-repeat.png"));
		
		toolBarTop->addSeparator();
		
		actionPlay=new QAction(tr("Play"), toolBarTop);
		toolBarTop->addAction(actionPlay);
		actionPlay->setIcon(style()->standardPixmap(QStyle::SP_MediaPlay));
		
		actionStop=new QAction(tr("Stop"), toolBarTop);
		toolBarTop->addAction(actionStop);
		actionStop->setIcon(style()->standardPixmap(QStyle::SP_MediaStop));
		actionStop->setEnabled(false);
		
		toolBarTop->addSeparator();
		
		actionFirst=new QAction(tr("First"), toolBarTop);
		toolBarTop->addAction(actionFirst);
		actionFirst->setIcon(style()->standardPixmap(QStyle::SP_MediaSkipBackward));
		
		actionPrevious=new QAction(tr("Previous"), toolBarTop);
		toolBarTop->addAction(actionPrevious);
		actionPrevious->setIcon(style()->standardPixmap(QStyle::SP_MediaSeekBackward));
		//actionPrevious->setEnabled(false);
		
		actionNext=new QAction(tr("Next"), toolBarTop);
		toolBarTop->addAction(actionNext);
		actionNext->setIcon(style()->standardPixmap(QStyle::SP_MediaSeekForward));
		//actionNext->setEnabled(false);
		
		actionLast=new QAction(tr("Last"), toolBarTop);
		toolBarTop->addAction(actionLast);
		actionLast->setIcon(style()->standardPixmap(QStyle::SP_MediaSkipForward));
}

void MainWindow::initializeStatusBar()
{
	labelStatus=new QLabel(tr("Not playing"));
	statusBar()->addWidget(labelStatus);
	
	labelTime=new QLabel("--:-- / --:--");
	statusBar()->addPermanentWidget(labelTime);
}

void MainWindow::initializeTabWidget()
{
	tabWidget=new QTabWidget(widgetCentral);
	tabWidget->setTabsClosable(true);
}

void MainWindow::initializeToolBarBottom()
{
	toolBarBottom=new QToolBar();
		/*labelTime=new QLabel("--:-- / --:--");
		toolBarBottom->addWidget(labelTime);
		
		toolBarBottom->addSeparator();*/
		
		seekSlider = new Phonon::SeekSlider;
		toolBarBottom->addWidget(seekSlider);
		volumeSlider=new Phonon::VolumeSlider;
		toolBarBottom->addWidget(volumeSlider);
}

void MainWindow::controlActions(const QString actionPlayText, bool enableActionStop)
{
	actionPlay->setText(actionPlayText);
	if(actionPlayText=="Pause")
	{
		actionPlay->setIcon(style()->standardPixmap(QStyle::SP_MediaPause));
	}
	else
	{
		actionPlay->setIcon(style()->standardPixmap(QStyle::SP_MediaPlay));
	}
	actionStop->setEnabled(enableActionStop);
}	
		
void MainWindow::onActionAboutTriggered()
{
	QMessageBox::information(this, tr("About NIA Audio Player"), tr("NIAAP is an open-source project developed by <strong>Guillaume BUREL</strong>, <br /><strong>Damien Garnier</strong> and <strong>Guillaume RABIER</strong>"));
}

QString MainWindow::displayTime(qint64 time)
{
	 QTime displayTime(0, (time/60000) % 60, (time/1000) % 60);
	 return displayTime.toString("mm:ss");
}

